package cloud.maque.querylist.restful.controller;

import cloud.maque.querylist.dal.domain.entity.Room;
import cloud.maque.querylist.dal.grpc.interfaces.RoomRpcService;
import cloud.maque.querylist.dal.interfaces.DemoService;
import cloud.maque.querylist.domain.conf.DomainProperties;
import cloud.maque.framework.common.response.ResponseBodyVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 根路由名称为 组件CODE,比如工作流：workflow
 * 如果本项目被项目直接引入，那么可以直接通过 /workflow来访问程序，不必使用http
 * 本文件可以删除
 *
 * @author zhuyf
 * @date 2022/6/28 15:52
 */
@RestController
@RequestMapping("/demo")
@RefreshScope //自动刷新nacos配置
public class DemoController {

    @Autowired
    DomainProperties properties;

    /**
     * @return
     */
    @GetMapping("")
    public String Test() {

        String names = "Hello,我是组件测试方法:" + properties.getName() + ",如果用了配置中心，请更新远程配置后刷新看效果";
        return names;
    }


    @Value("${server.servlet.application-display-name}")
    String displayName;

    @GetMapping("/name")
    public String Hello() {
        return displayName;
    }

    @Autowired
    DemoService demoService;

    @GetMapping("/demo")
    public ResponseBodyVo Demo(HttpServletResponse response) {

        return new ResponseBodyVo(demoService.DemoMehtod());
    }

    /**
     * 返回一个 RPC 数据
     *
     * @param response
     * @return
     */
    @GetMapping("/rpc")
    public ResponseBodyVo Rpc(HttpServletResponse response) {

        return new ResponseBodyVo(demoService.demoEvent());
    }


    @Autowired
    RoomRpcService rpcService;

    /**
     * @return 添加一个实体数据
     */
    @GetMapping("/roomAdd")
    public ResponseBodyVo Add() {

        Room room = new Room();
        room.setRoomId("room2");
        room.setRoomName("教室2");

        return new ResponseBodyVo(rpcService.roomAdd(room));
    }


    /**
     * @return 添加一个实体数据
     */
    @GetMapping("/list")
    public ResponseBodyVo GetData() {
        return new ResponseBodyVo(rpcService.roomList());
    }


}
